'use strict';

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

var vue = new Vue({
    data: {
        search: {
            page: 1,
            itemid: '',
            catid: 0,
            title: '',
            flag: '0'
        },
        category_select: [],
        loading: false,
        tableData: [],
        total: 0,
        checked_items: [],
        batch: {
            catid: 0,
            flag: '0'
        },
        dialogVisible: false,
        export_percent: '',
        show_download: false,
        download_url: ''
    },
    methods: {
        handleSelectionChange: function handleSelectionChange(val) {
            this.checked_items = val;
        },

        //加载数据
        load_data: function load_data(cb) {
            var _this = this;

            this.loading = true;
            axios.post('/api/article/lst', {
                page: this.search.page,
                itemid: this.search.itemid,
                catid: this.search.catid,
                title: this.search.title,
                flag: this.search.flag
            }).then(function (response) {
                var data = response.data;
                _this.loading = false;
                _this.tableData = data.data.rows;
                _this.total = data.data.total;
                if (cb) {
                    cb();
                }
                _this.$forceUpdate();
            }).catch(function (error) {
                _this.loading = false;
                _this.$message.error(error.toString());
            });
        },

        //分页点击
        handleCurrentChange: function handleCurrentChange(page) {
            this.search.page = page;
            this.load_data();
            this.$paging();
        },

        //搜索
        to_search: function to_search() {
            var c = 0;
            if (parseInt(this.search.itemid) > 0) {
                c++;
            }
            if (parseInt(this.search.catid) > 0) {
                c++;
            }
            if (this.search.title.length > 0) {
                c++;
            }
            if (parseInt(this.search.flag) > 0) {
                c++;
            }
            if (!c) {
                this.$message.error('请输入搜索条件！');
                return;
            }
            this.search.page = 1;
            this.load_data();
        },

        //重置
        to_reset: function to_reset() {
            this.search = {
                page: 1,
                itemid: '',
                catid: 0,
                title: '',
                flag: '0'
            };
            this.load_data();
        },

        //导出Excel
        to_export: function to_export() {
            var _this2 = this;

            var request = function () {
                var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(filename, page) {
                    var response, data;
                    return regeneratorRuntime.wrap(function _callee$(_context) {
                        while (1) {
                            switch (_context.prev = _context.next) {
                                case 0:
                                    _context.prev = 0;
                                    _context.next = 3;
                                    return axios.post('/api/article/lst', {
                                        is_export: 1,
                                        filename: filename,
                                        page: page,
                                        itemid: _this2.search.itemid,
                                        catid: _this2.search.catid,
                                        title: _this2.search.title,
                                        flag: _this2.search.flag
                                    });

                                case 3:
                                    response = _context.sent;
                                    data = response.data;

                                    _this2.dialogVisible = true;
                                    _this2.export_percent = data.data.progress;

                                    if (!data.data.status) {
                                        _context.next = 11;
                                        break;
                                    }

                                    _this2.show_download = true;
                                    _this2.download_url = data.data.browser_path;
                                    return _context.abrupt('return');

                                case 11:
                                    //下一页
                                    request(data.data.filename, page + 1);
                                    _context.next = 17;
                                    break;

                                case 14:
                                    _context.prev = 14;
                                    _context.t0 = _context['catch'](0);

                                    _this2.$message.error(_context.t0.toString());

                                case 17:
                                    return _context.abrupt('return', true);

                                case 18:
                                case 'end':
                                    return _context.stop();
                            }
                        }
                    }, _callee, _this2, [[0, 14]]);
                }));

                return function request(_x, _x2) {
                    return _ref.apply(this, arguments);
                };
            }();

            this.export_percent = '';
            this.show_download = false;
            this.download_url = '';
            request('', 1);
        },
        handleClose: function handleClose(done) {
            this.$confirm('确认关闭？').then(function (_) {
                done();
            }).catch(function (_) {});
        },

        //预览
        handlePreview: function handlePreview(index, row) {
            this.$forward('/admin/article/preview?itemid=' + row.itemid);
        },

        //编辑
        handleEdit: function handleEdit(index, row) {
            this.$forward('/admin/article/mod?itemid=' + row.itemid);
        },

        //删除
        handleDelete: function handleDelete(index, row) {
            this.$forward('/admin/article/del?itemid=' + row.itemid);
            return;

            /*
                     this.$confirm('确定要删除吗？', '系统提醒', {
                         confirmButtonText: '确定',
                         cancelButtonText: '取消',
                         type: 'warning'
                     }).then(() => {
                         this.loading = true;
                         axios.post('/api/article/del', {itemid: row.itemid}).then((response) => {
                             this.loading = false;
                             var data = response.data;
                             if (parseInt(data.code) > 0) {
                                 this.tableData.splice(index, 1);
                                 this.$message({
                                     type: 'success',
                                     message: '删除成功!'
                                 });
                               } else {
                                 this.$message({
                                     type: 'error',
                                     message: '删除失败!'
                                 });
                             }
                         }).catch((error) => {
                             this.loading = false;
                             this.$message.error(error.toString());
                         });
                       }).catch(() => {
                       });
            */
        },

        //批量更新
        batch_update_catid: function batch_update_catid() {
            var _this3 = this;

            if (!this.checked_items.length) {
                this.$message.error('请勾选数据！');
                return;
            }
            if (!parseInt(this.batch.catid)) {
                this.$message.error('请选择状态！');
                return;
            }
            var itemids = this.checked_items.map(function (v, k) {
                return v.itemid;
            });
            axios.post('/api/article/batch_update', {
                action: 'catid',
                itemids: itemids,
                catid: this.batch.catid
            }).then(function (response) {
                var data = response.data;
                if (parseInt(data.code) > 0) {
                    _this3.batch.catid = 0;
                    _this3.$message({
                        type: 'success',
                        message: '操作成功!'
                    });
                    setTimeout(function () {
                        _this3.load_data();
                    }, 500);
                } else {
                    _this3.$message({
                        type: 'error',
                        message: '操作失败!'
                    });
                }
            }).catch(function (error) {
                _this3.$message.error(error.toString());
            });
        },
        batch_update_flag: function batch_update_flag() {
            var _this4 = this;

            if (!this.checked_items.length) {
                this.$message.error('请勾选数据！');
                return;
            }
            if (!parseInt(this.batch.flag)) {
                this.$message.error('请选择状态！');
                return;
            }
            var itemids = this.checked_items.map(function (v, k) {
                return v.itemid;
            });
            axios.post('/api/article/batch_update', {
                action: 'flag',
                itemids: itemids,
                flag: this.batch.flag
            }).then(function (response) {
                var data = response.data;
                if (parseInt(data.code) > 0) {
                    _this4.batch.flag = '0';
                    _this4.$message({
                        type: 'success',
                        message: '操作成功!'
                    });
                    setTimeout(function () {
                        _this4.load_data();
                    }, 500);
                } else {
                    _this4.$message({
                        type: 'error',
                        message: '操作失败!'
                    });
                }
            }).catch(function (error) {
                _this4.$message.error(error.toString());
            });
        }
    },
    created: function created() {},
    mounted: function () {
        var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
            var params, search, response, data;
            return regeneratorRuntime.wrap(function _callee2$(_context2) {
                while (1) {
                    switch (_context2.prev = _context2.next) {
                        case 0:
                            this.$check_login('article', 'lst');
                            //地址栏参数
                            params = Qs.parse(window.location.search.substr(1));

                            if (typeof params.itemid != 'undefined' && parseInt(params.itemid) > 0) {
                                this.search.itemid = parseInt(params.itemid);
                            }
                            if (typeof params.title != 'undefined' && params.title) {
                                this.search.title = title;
                            }
                            if (typeof params.flag != 'undefined' && parseInt(params.flag) > 0) {
                                this.search.flag = parseInt(params.flag);
                            }

                            //sessionStorage
                            try {
                                if (sessionStorage.hasOwnProperty(location.pathname)) {
                                    search = sessionStorage.getItem(location.pathname);

                                    this.search = JSON.parse(search);
                                }
                            } catch (e) {
                                console.error(e);
                            }
                            //加载数据
                            this.load_data();

                            _context2.prev = 7;
                            _context2.next = 10;
                            return axios.post('/api/article/category_lst', {});

                        case 10:
                            response = _context2.sent;
                            data = response.data;

                            if (parseInt(data.code) > 0) {
                                this.category_select = data.data;
                            } else {
                                this.$message.error(data.msg);
                            }
                            _context2.next = 18;
                            break;

                        case 15:
                            _context2.prev = 15;
                            _context2.t0 = _context2['catch'](7);

                            this.$message.error(_context2.t0.toString());

                        case 18:
                        case 'end':
                            return _context2.stop();
                    }
                }
            }, _callee2, this, [[7, 15]]);
        }));

        function mounted() {
            return _ref2.apply(this, arguments);
        }

        return mounted;
    }()
}).$mount('#app');